package stockService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;


@WebServlet(urlPatterns = {"/btn"})
public class btnService extends HttpServlet {
    private static final long serialVersionUID = 1L;

    static String btn = "";
    static int count;

    public static void setButton() {
        btn = "";
        count = loadData.count;
        loadData[] stock  = loadData.stocks;
        btn = btn + count;
        btn = btn + ",";
        for(int i = 0; i < count; i++){
            btn = btn + new DecimalFormat("0.00%").format(stock[i].perc);
            btn = btn + ",";
            if(stock[i].perc < 0){
                btn = btn + "-";
            }
            else{
                btn = btn + "+";
            }
            btn = btn + ",";
            btn = btn + stock[i].code;
            btn = btn + ",";
            btn = btn + stock[i].name;
            btn = btn + ",";
            btn = btn + stock[i].current;
            btn = btn + ",";
            btn = btn + stock[i].today;
            btn = btn + ",";
            btn = btn + stock[i].last;
            btn = btn + ",";
            btn = btn + stock[i].highest;
            btn = btn + ",";
            btn = btn + stock[i].lowest;
            btn = btn + ",";
        }
    }

    public String getButton() {
        return btn;
    }


    public btnService() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html; charset=utf-8");
        PrintWriter out = response.getWriter();
        try {
            response.setHeader("Cache-Control", "no-store");
            response.setHeader("Pragma", "no-cache");
            response.setDateHeader("Expires", 0);
            setButton();
            out = response.getWriter();
            out.println(getButton());
        } catch (Exception e) {
            out.print("error: btn");
            e.printStackTrace();
        }

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
}